/*
 * 文件名：UserMessageSerializer.java
 * 版权：
 * 描述：TODO
 * 修改人：zuo liubo
 * 修改时间：2013-5-24 下午2:16:06
 */
package com.scpii.api.common.json.serializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

import com.scpii.api.common.Constants;
import com.scpii.api.domain.content.Body;
import com.scpii.api.domain.push.UserMessages;

/**
 * @description TODO
 * @author zuo liubo
 * @date 2013-5-24
 */
public class UserMessageSerializer extends JsonSerializer<UserMessages> {

	@Override
	public void serialize(UserMessages value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
			JsonProcessingException {
		jgen.writeStartObject();
		jgen.writeNumberField("userMessageId", value.getUserMessageId());
		jgen.writeNumberField("messageId", value.getMessage().getMessageId());
		jgen.writeNumberField("appId", value.getMessage().getAppId());
		jgen.writeStringField("title", value.getMessage().getTitle());
		if (value.getMessage().getIcon() != null)
			jgen.writeStringField("icon", value.getMessage().getIcon());
		jgen.writeStringField("summary", value.getMessage().getSummary());
		jgen.writeArrayFieldStart("body");
		List<Body> bodys = value.getMessage().getBody();
		for (Body body : bodys) {
			jgen.writeObject(body);
		}
		jgen.writeEndArray();

		Date startTime  = value.getMessage().getStartTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		jgen.writeStringField("startTime",sdf.format(startTime));
		jgen.writeNumberField("messageStatus", value.getMessageStatus() );
		jgen.writeNumberField("intervalTime", Integer.parseInt(Constants.PUSH_INTERVAL_TIME));
		jgen.writeEndObject();
	}
}
